%% 幻觉标准刺激(连接线颜色与背景相同)，添加颜色通道和对比度增强的背景色，按颜色保存到不同文件夹
clear; sca
[xax, yax] = meshgrid(1:224, 1:224);
rax = xax+1i*yax;

%% 设置半径
circle_radius = 6; % 单点圆半径R=6

%% 设置图片数和颜色数
number_sets = 1:15; % 要生成15类数量
image_iter = 20; % 每类数量要生成20张图
color_num = 50; % 颜色种类数量

% 生成50种随机颜色
colors = rand(color_num, 3); % 每行是一种RGB颜色
colors = colors * 0.8 + 0.2; % 确保颜色值在0.2到1.0之间

% 创建主输出文件夹
main_output_dir = 'illusion_standard_images';
if ~exist(main_output_dir, 'dir')
    mkdir(main_output_dir);
end

% 初始化存储数组 (224*224*3*20*15*50)
illusion_image_standard = zeros(size(rax, 1), size(rax, 2), 3, image_iter, length(number_sets), color_num); 

for cc = 1:color_num % 对每种颜色循环
    current_color = colors(cc, :); % 当前颜色
    
    % 使用HSV色彩空间计算背景颜色
    hsv_color = rgb2hsv(current_color);
    hsv_color(2) = hsv_color(2) * 0.4;   % 降低饱和度
    hsv_color(3) = min(1, hsv_color(3) * 1.8);  % 增加明度
    bg_color = hsv2rgb(hsv_color);
    
    % 检查颜色差值，确保差值>100 (0-255范围内)
    stim_color_255 = round(current_color * 255);
    bg_color_255 = round(bg_color * 255);
    color_diff = sqrt(sum((stim_color_255 - bg_color_255).^2));
    
    % 调整背景色直到满足差值要求
    adjustment_attempts = 0;
    while color_diff < 100 && adjustment_attempts < 10
        if hsv_color(3) < 0.9
            hsv_color(3) = min(1, hsv_color(3) + 0.1);
        else
            hsv_color(2) = max(0, hsv_color(2) - 0.1);
        end
        bg_color = hsv2rgb(hsv_color);
        bg_color_255 = round(bg_color * 255);
        color_diff = sqrt(sum((stim_color_255 - bg_color_255).^2));
        adjustment_attempts = adjustment_attempts + 1;
    end
    
    if color_diff < 100
        luminance = 0.2126 * current_color(1) + 0.7152 * current_color(2) + 0.0722 * current_color(3);
        if luminance > 0.5
            bg_color = [0.1, 0.1, 0.1];
        else
            bg_color = [0.9, 0.9, 0.9];
        end
    end
    
    % 创建颜色文件夹
    color_folder_name = sprintf('color_%d_%.3f_%.3f_%.3f_diff%.0f', cc, current_color(1), current_color(2), current_color(3), color_diff);
    color_folder_path = fullfile(main_output_dir, color_folder_name);
    if ~exist(color_folder_path, 'dir')
        mkdir(color_folder_path);
    end
    
    for tt = 1:length(number_sets) % 类数量循环
        nn = number_sets(tt); % 当前类连接圆个数

        for ii = 1:image_iter % 每类生成20张图
            % 创建背景图像
            img = ones(224, 224, 3);
            for ch = 1:3
                img(:,:,ch) = bg_color(ch);
            end
            
            % 保存圆心和连接信息
            all_centers_x = [];
            all_centers_y = [];
            connections = [];
            
            % 生成连接圆
            while size(connections, 1) < nn
                x1 = 5*ceil(circle_radius) + randi(size(rax,2) - 10*ceil(circle_radius));
                y1 = 5*ceil(circle_radius) + randi(size(rax,1) - 10*ceil(circle_radius));
                
                theta = 2*pi*rand;
                r_distance = 4*circle_radius;
                
                x2 = x1 + r_distance * cos(theta);
                y2 = y1 + r_distance * sin(theta);
                
                if x2 < 1 || x2 > size(rax,2) || y2 < 1 || y2 > size(rax,1)
                    continue;
                end
                
                xm = (x1 + x2) / 2;
                ym = (y1 + y2) / 2;
                
                too_close = false;
                for j = 1:size(connections, 1)
                    idx1 = connections(j, 1);
                    idx2 = connections(j, 2);
                    x_mid = (all_centers_x(idx1) + all_centers_x(idx2)) / 2;
                    y_mid = (all_centers_y(idx1) + all_centers_y(idx2)) / 2;
                    dist = sqrt((xm - x_mid)^2 + (ym - y_mid)^2);
                    if dist < 6*circle_radius
                        too_close = true;
                        break;
                    end
                end
                
                if too_close
                    continue;
                end
                
                idx1 = length(all_centers_x) + 1;
                idx2 = length(all_centers_x) + 2;
                all_centers_x = [all_centers_x, x1, x2];
                all_centers_y = [all_centers_y, y1, y2];
                connections = [connections; idx1, idx2];
            end
            
            % 绘制圆点
            for i = 1:length(all_centers_x)
                img = drawCircle(img, round(all_centers_x(i)), round(all_centers_y(i)), circle_radius, current_color);
            end

            % 改进后的连接线绘制
            for c = 1:size(connections, 1)
                idx1 = connections(c, 1);
                idx2 = connections(c, 2);
                
                x1 = round(all_centers_x(idx1));
                y1 = round(all_centers_y(idx1));
                x2 = round(all_centers_x(idx2));
                y2 = round(all_centers_y(idx2));
                
                img = drawLine(img, x1, y1, x2, y2, bg_color);
            end
            
            % 保存图像
            illusion_image_standard(:,:,:,ii,tt,cc) = img;
            img_filename = sprintf('illusion_nn%d_img%d.png', nn, ii);
            img_path = fullfile(color_folder_path, img_filename);
            imwrite(img, img_path);
            
            % 显示示例图像
            if cc == 1 && tt == 1 && ii == 1
                figure;
                imshow(img);
                title(sprintf('幻觉刺激示例图像 - 颜色差值: %.0f', color_diff));
                drawnow;
            end
        end
    end
end

% 保存变量
illusion_vgg_standard_color = illusion_image_standard;
save illusion_vgg_standard_color.mat illusion_vgg_standard_color -v7.3

% 改进后的画线函数
function img = drawLine(img, x1, y1, x2, y2, color)
    % 确保起点和终点在图像范围内
    x1 = max(1, min(size(img, 2), round(x1)));
    y1 = max(1, min(size(img, 1), round(y1)));
    x2 = max(1, min(size(img, 2), round(x2)));
    y2 = max(1, min(size(img, 1), round(y2)));
    
    % 计算距离和步长
    dx = x2 - x1;
    dy = y2 - y1;
    steps = max(abs(dx), abs(dy));
    if steps == 0
        return;
    end
    
    x_step = dx / steps;
    y_step = dy / steps;
    
    % 绘制线段
    for i = 0:steps
        x = round(x1 + i * x_step);
        y = round(y1 + i * y_step);
        if x >= 1 && x <= size(img, 2) && y >= 1 && y <= size(img, 1)
            img(y, x, 1) = color(1);
            img(y, x, 2) = color(2);
            img(y, x, 3) = color(3);
        end
    end
end

% 自定义画圆函数
function img = drawCircle(img, cx, cy, r, color)
    [X, Y] = meshgrid(1:size(img, 2), 1:size(img, 1));
    mask = (X - cx).^2 + (Y - cy).^2 <= r^2;
    for c = 1:3
        channel = img(:, :, c);
        channel(mask) = color(c);
        img(:, :, c) = channel;
    end
end